package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.constants.RedisConstants;
import com.heima.common.exception.CustomException;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.LoginDto;
import com.heima.model.user.dtos.UserRelationDto;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.user.vo.LoginVO;
import com.heima.user.mapper.ApUserMapper;
import com.heima.user.service.UserService;
import com.heima.utils.common.AppJwtUtil;
import com.heima.utils.thread.AppThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundSetOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.nio.charset.StandardCharsets;

/**
 * @author brianxia
 * @version 1.0
 * @date 2022-11-08 14:30
 */
@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<ApUserMapper,ApUser> implements UserService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public LoginVO login(LoginDto loginDto) {

        LoginVO loginVO = new LoginVO();

        if(StringUtils.isBlank(loginDto.getPhone()) || StringUtils.isBlank(loginDto.getPassword())){
            //2.游客登录

            loginVO.setToken(AppJwtUtil.getToken(0L));


        }else{
           //1.正常登陆 手机号和密码
            ApUser user = getOne(Wrappers.<ApUser>lambdaQuery().
                    eq(ApUser::getPhone, loginDto.getPhone()));
            if(user == null){
                throw new CustomException(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
            }

            //对前端密码进行加密
            String pwdFront = loginDto.getPassword();//前端密码，未加密
            String salt = user.getSalt();//盐
            //加密后的前端密码
            String pwdFront2 = DigestUtils.md5DigestAsHex((pwdFront + salt).getBytes(StandardCharsets.UTF_8));
            if(!pwdFront2.equals(user.getPassword())){
                throw new CustomException(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
            }

            LoginVO.UserDTO userDTO = new LoginVO.UserDTO();
            BeanUtils.copyProperties(user,userDTO);
            loginVO.setUser(userDTO);
            loginVO.setToken(AppJwtUtil.getToken(Long.valueOf(user.getId())));
        }

        return loginVO;
    }

    @Override
    public void follow(UserRelationDto dto) {
        //点赞
        ApUser user = AppThreadLocalUtil.getUser();
        if(user == null){
            //没登录
            throw new CustomException(AppHttpCodeEnum.NEED_LOGIN);
        }
        //1.获取用户id
        Integer userId = user.getId();

        //2.存储到redis

        //登录用户关注了哪些作者
        BoundSetOperations<String, String> bound1 =
                stringRedisTemplate.boundSetOps(RedisConstants.FOLLOW_USER_KEY + userId.toString());
        //作者有哪些粉丝
        BoundSetOperations<String, String> bound2 =
                stringRedisTemplate.boundSetOps(RedisConstants.FOLLOW_AUTHOR_KEY + dto.getAuthorId().toString());
        if(dto.getOperation() == 0){
            //点赞
            bound1.add(dto.getAuthorId().toString());
            bound2.add(userId.toString());

        }else if(dto.getOperation() == 1){
            //取消点赞
            bound1.remove(dto.getAuthorId().toString());
            bound2.remove(userId.toString());
        }
//        //粉丝数量
//        Long size = bound2.size();

    }
}
